package com.wizinno.android.urineflow.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.wizinno.android.urineflow.R;

/**
 * @author LiuMei
 * @date 2020-12-24.
 */
public class CustomDialog extends Dialog {

    private Button btn_dialog_negative;
    private Button btn_dialog_positive;

    private TextView tv_dialog_title;
    private TextView tv_dialog_message;

    private Context context;

    public CustomDialog(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//            final CustomDialog dialog = new CustomDialog(context,R.style.Dialog);
        this.context = context;
        View layout = inflater.inflate(R.layout.dialog_normal, null);
        addContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        tv_dialog_title = layout.findViewById(R.id.tv_dialog_title);
        tv_dialog_message = layout.findViewById(R.id.tv_dialog_message);

        btn_dialog_negative = layout.findViewById(R.id.btn_dialog_negative);
        btn_dialog_positive = layout.findViewById(R.id.btn_dialog_positive);

        setNegativeButton((dialog, which) -> {dialog.dismiss();});

        setContentView(layout);
    }

    public void setMessage(String message) {
        tv_dialog_message.setText(message);
    }

    public void setPositiveButton(DialogInterface.OnClickListener onClickListener) {
        CustomDialog customDialog = this;
        btn_dialog_positive.setOnClickListener(v -> onClickListener.onClick(customDialog, DialogInterface.BUTTON_POSITIVE));
    }

    public void setNegativeButton(DialogInterface.OnClickListener onClickListener) {
        CustomDialog customDialog = this;
        btn_dialog_negative.setOnClickListener(v -> onClickListener.onClick(customDialog, DialogInterface.BUTTON_NEGATIVE));
    }

    private void fullScreenImmersive(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(uiOptions);
        }
    }

    @Override
    public void show() {
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        super.show();
        fullScreenImmersive(getWindow().getDecorView());
        this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    }
}
